<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/18 0018
 * Time: 11:31
 */

namespace app\index\controller\identityTesting;


use think\Controller;
use app\common\controller\order\Orderget;
class VerifyInfo extends Controller
{
    private $number = '';
    /*
     * 验证方法
     * */
    public function totle($var = ''){
        if ($var == '') {
            $this->show_error_api($message = '身份证号不能为空');
        }
        $this->number = $var;
        $this->province();
//        获取年龄
        $age = $this->getAgeByID();
        $basicinfo=(new Orderget())->get_basicinfo();//获取系统高配置数据
	    if ($age > $basicinfo['MaxAges']){
            $this->show_error_api('年龄超过不符合借款规定');
	    }
		 if ( $age < $basicinfo['MinAges']){
             $this->show_error_api('未成年应该好好学习');
	    }
    }
    /*
     * 验证省份信息
     * */
    private function province(){
        $str = substr($this->number,0,2);
        $find = db('address')->where(['areaCode'=>$str])->find();
        if (empty($find)) $this->show_error_api('未知的省份');
        if ($find['status'] == 0) $this->show_error_api($find['name'].'已被限制使用');
    }
    /*
     * 获取年龄
     * */
    private function getAgeByID(){
        $date=strtotime(substr($this->number,6,8));
//      获得出生年月日的时间戳
        $today=strtotime('today');
//      获得今日的时间戳
        $diff=floor(($today-$date)/86400/365);
//      得到两个日期相差的大体年数
//      strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比
        $age=strtotime(substr($this->number,6,8).' +'.$diff.'years')>$today?($diff+1):$diff;
        return $age;
    }
    // 删除 修改 添加 状态返回  失败
    private function show_error_api($message='操作失败', $status='520', $data=[]){
        $reuslt=array(
            'status'=>$status,
            'msg'=>$message,
            'data'=>$data,
        );
        exit(json_encode($reuslt,JSON_UNESCAPED_UNICODE));
    }
}